package com.cgycms.webdeploy.controller;

import cn.hutool.core.io.FileUtil;
import com.cgycms.webdeploy.aop.Auth;
import com.cgycms.webdeploy.aop.Log;
import com.cgycms.webdeploy.auth.RolesEnum;
import com.cgycms.webdeploy.common.Result;
import com.cgycms.webdeploy.ftp.FtpUtil;
import com.cgycms.webdeploy.storage.DeployConfig;
import com.cgycms.webdeploy.storage.ServersPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @ClassName : HomeController
 * @Description : 首页
 * @Author : 王力超
 * @Date: 2020-11-05 16:06
 */
@Controller
public class HomeController extends BaseController {


    @Autowired
    private DeployConfig deployConfig;

    /**
     * 框架首页
     *
     * @return
     */
    @RequestMapping(value = {"/", "/index", "/index.html"})
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("user", user());
        return mv;
    }


    /**
     * 主页
     *
     * @return
     */
    @RequestMapping(value = "/page/welcome")
    public ModelAndView welcome() {
        ModelAndView mv = new ModelAndView("welcome");
        return mv;
    }


    /**
     * 发布模板页面
     *
     * @return
     */
    @Auth(value = "发布上传页面", roles = {RolesEnum.ADMIN, RolesEnum.DEPLOY_ADMIN})
    @RequestMapping(value = "/page/uploadPage")
    public ModelAndView uploadPage() {
        ModelAndView mv = new ModelAndView("deployUpload");
        return mv;
    }

    /**
     * 服务管理页面
     *
     * @return
     */
    @Log(value = "服务管理页面", type = 2)
    @RequestMapping(value = "/page/serverPage")
    public ModelAndView serverPage() {
        ModelAndView mv = new ModelAndView("serverPage");
        mv.addObject("groupNumber", ServersPool.getServerGroupNumber());
        mv.addObject("serverNumber", ServersPool.getServerNumber());
        return mv;
    }


    /**
     * 服务管理列表
     *
     * @return
     */
    @RequestMapping("/getServerData")
    @ResponseBody
    public String getServerData() {
        List<FtpUtil> serverAll = ServersPool.getServersWheres(null, null, null, true);
        return Result.ok(serverAll);
    }

    /**
     * 文件管理页面
     *
     * @return
     */
    @Log(value = "文件管理页面", type = 2)
    @Auth(value = "文件管理页面", roles = {RolesEnum.ADMIN, RolesEnum.FILE_ADMIN})
    @RequestMapping(value = "/page/filePage")
    public ModelAndView filePage() {
        ModelAndView mv = new ModelAndView("filePage");
        mv.addObject("groupNumber", ServersPool.getServerGroupNumber());
        mv.addObject("serverNumber", ServersPool.getServerNumber());
        return mv;
    }


    /**
     * 登录日志页面
     *
     * @return
     */
    @Auth(value = "登录日志页面", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN})
    @RequestMapping(value = "/page/loginLogPage")
    public ModelAndView loginLogPage() {
        ModelAndView mv = new ModelAndView("loginLogPage");
        return mv;
    }


    /**
     * 操作日志页面
     *
     * @return
     */
    @Auth(value = "操作日志页面", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN})
    @RequestMapping(value = "/page/operationLogPage")
    public ModelAndView operationLogPage() {
        ModelAndView mv = new ModelAndView("operationLogPage");
        return mv;
    }

    /**
     * 数据管理页面
     *
     * @return
     */
    @Auth(value = "数据管理页面", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN})
    @Log(value = "数据管理页面", type = 2)
    @RequestMapping(value = "/page/dataBasePage")
    public ModelAndView dataBasePage() {
        ModelAndView mv = new ModelAndView("dataBasePage");
        String logPath = deployConfig.getLogPath();
        String tempPath = deployConfig.getTempPath();
        double logSize = FileUtil.size(FileUtil.newFile(logPath)) / 1024;
        double tempSize = FileUtil.size(FileUtil.newFile(tempPath)) / 1024;
        mv.addObject("logPath", logPath);
        mv.addObject("tempPath", tempPath);
        mv.addObject("logSize", String.format("%.2f", logSize));
        mv.addObject("tempSize", String.format("%.2f", tempSize));
        return mv;
    }

}
